package com.matrix.quartz.action;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.quartz.bean.QuartzExecuteHistory;
import com.matrix.quartz.dao.QuartzExecuteHistoryDao;

/**
 * @description
 * @author zhangheng
 * @date 2019-01-14 15:37
 */
@Controller
@RequestMapping(value = "developer/quartzExecuteHistory")
public class QuartzExecuteHistoryAction {

	@Autowired
	private QuartzExecuteHistoryDao quartzExecuteHistoryDao;
	
	//
	public static final String BEV="QuartzExecuteHistory_BEV";
	
	private static final String USER_NAME="SYS";
	
	/**
	 *
	 */
	@RequestMapping(value =  "/showList")
	public @ResponseBody AjaxResult showList(QuartzExecuteHistory quartzExecuteHistory, PaginationVO pageVo) {

		if(StringUtils.isBlank(pageVo.getSort())) {
			pageVo.setOrder("desc");
			pageVo.setSort("create_time");
		}
		List<QuartzExecuteHistory> dataList = quartzExecuteHistoryDao.selectInPage(quartzExecuteHistory, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
				quartzExecuteHistoryDao.selectTotalRecord(quartzExecuteHistory));
		return result;
	}
   	
	/**
	 *
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value =  "/addQuartzExecuteHistory")
	public @ResponseBody AjaxResult addQuartzExecuteHistory(QuartzExecuteHistory quartzExecuteHistory) {
		quartzExecuteHistory.setCreateBy(USER_NAME);
		quartzExecuteHistory.setUpdateBy(USER_NAME);
	 	int i=quartzExecuteHistoryDao.insert(quartzExecuteHistory);
	 	if(i > 0){
	 		return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "");
	 	}else {
			throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
		}
	}
	
	
	
	
	
	/**
	 *
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value =  "/modifyQuartzExecuteHistory")
	public @ResponseBody AjaxResult modifyQuartzExecuteHistory(QuartzExecuteHistory newQuartzExecuteHistory) {
	   	QuartzExecuteHistory oldQuartzExecuteHistory = WebUtil.getSessionAttribute(BEV);
		int i = 0;
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldQuartzExecuteHistory, newQuartzExecuteHistory)) {
				i = MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldQuartzExecuteHistory, newQuartzExecuteHistory);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newQuartzExecuteHistory);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("ehId", oldQuartzExecuteHistory.getEhId());
			quartzExecuteHistoryDao.updateByMap(modifyMap);
		}
		i = MatrixConstance.DML_SUCCESSS;
		WebUtil.removeSessionAttribute(BEV);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
		}
	}
	
	
	
	
   	/**
	 *
	 */   
	@SaveRequestToken
   	@RequestMapping(value =  "/editForm")
	public String editForm(Integer id) {
		QuartzExecuteHistory quartzExecuteHistory;
		if (id != null) {
			quartzExecuteHistory = quartzExecuteHistoryDao.selectById(id);
			WebUtil.getRequest().setAttribute("obj", quartzExecuteHistory);
			WebUtil.setSessionAttribute(BEV, quartzExecuteHistory);
		}
		return "developer/QuartzExecuteHistory-form";
	}
   	
   	
   	/**
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<String> ids = StringUtils.strToCollToString(keys, ",");
		int i =  quartzExecuteHistoryDao.deleteByIds(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i);
		} else {
			throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
		}
	}
  
}